package com.huanxun.foodspath3;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.huanxun.application.MApplication;
import com.huanxun.fragment.MsgListFragment;
import com.huanxun.fragment.NetConnectiveFailedFragment;
import com.huanxun.utils.NetConnectiveUils;

/**
 * 消息Activity
 *
 */
public class MessageActivity extends FragmentActivity {

	static final int MENU_MANUAL_REFRESH = 0;
	static final int MENU_DISABLE_SCROLL = 1;
	static final int MENU_SET_MODE = 2;
	static final int MENU_DEMO = 3;

	private long exitTime = 0;
	private ImageView iv_user;

	public String roleNumber;
	public String id;
	private String loginname;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message);
		((MApplication) getApplication()).activities.add(this);
		Intent intent = getIntent();
		roleNumber = intent.getStringExtra("roleNumber");
		id = intent.getStringExtra("id");
		loginname = intent.getStringExtra("loginname");
		initView();
		FragmentManager supportFragmentManager = getSupportFragmentManager();
		FragmentTransaction beginTransaction = supportFragmentManager
				.beginTransaction();
		if (NetConnectiveUils.isNetworkAvailable(MessageActivity.this)) {
			beginTransaction.replace(R.id.fl_msg, new MsgListFragment())
					.commit();
		} else {
			beginTransaction.replace(R.id.fl_msg,
					new NetConnectiveFailedFragment()).commit();
		}
	}

	private void initView() {
		iv_user = (ImageView) findViewById(R.id.iv_user);
		iv_user.setOnClickListener(new ClickListener());

	}

	private class ClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.putExtra("loginname", loginname);
			intent.setClass(MessageActivity.this, UserActivity.class);
			startActivity(intent);
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				Toast.makeText(getApplicationContext(), "再按一次退出程序",
						Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				MApplication application = (MApplication) getApplication();
				List<Activity> activities = application.activities;
				for (Activity activity : activities) {
					activity.finish();
				}
				finish();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void runOnUiPrompt(final String text) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(MessageActivity.this, text, Toast.LENGTH_SHORT)
						.show();
			}
		});
	}
}
